//
//  XGL_GiftAnimationView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/30.
//

import UIKit

class XGL_GiftAnimationView: UIView {

    private var contentView: UIView!
    private var colorView: UIView!
    private var count: NSInteger = -1
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.alpha = 0
        self.contentView = UIView()
        self.colorView = UIView().then({ make in
            make.layerCornerRadius = 12.0
        })
        self.addSubviews([self.colorView,self.contentView])
        self.colorView.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.center.equalToSuperview()
            make.height.equalTo(24.0)
        }
        self.contentView.snp.makeConstraints { make in
            make.center.height.equalToSuperview()
            make.left.equalTo(15.0)
        }
        PanTimer.share.scheduledDispatchTimer(withName: "GiftAnimationTimer", timeInterval: 1, queue: .main, repeats: true) { timeInterval in
            if self.count > 0{
                self.count -= 1
            }else if self.count == 0{
                self.dismiss()
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.colorView.layoutIfNeeded()
        self.colorView.addGradientLayer(start: CGPoint(x: 0, y: 0.5), end: CGPoint(x: 1, y: 0.5), frame: self.colorView.bounds, colors: [UIColor.hexColor("#83FFE7").cgColor,UIColor.hexColor("#001310").cgColor])
    }
    
    deinit {
        PanTimer.share.destoryTimer(withName: "GiftAnimationTimer")
    }
}
extension XGL_GiftAnimationView{
    func showGiftAnimation(message: String,giftData: GiftData){
        self.count = 5
        self.contentView.removeSubviews()
        let giftImageView = UIImageView().then { make in
            make.loadImageWithUrl(giftData.imgUrl)
            make.layerCornerRadius = 25.0
            make.clipsToBounds = true
        }
        let giftNumberText = UILabel().then { make in
            make.text = "X1"
            make.textColor = .white
            make.textAlignment = .center
            make.font = UIFont.pingfangSC_Regular(fontSize: 13.0)
        }
        let messageText = UILabel().then { make in
            make.text = message
            make.textColor = UIColor.hexColor("#191A1D")
            make.font = UIFont.pingfangSC_Regular(fontSize: 12.0)
        }
        self.contentView.addSubviews([messageText,giftImageView,giftNumberText])
        
        giftNumberText.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.equalTo(45.0)
            make.right.equalTo(-10.0)
            make.height.equalTo(20)
        }
        giftImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.height.equalTo(50.0)
            make.right.equalTo(giftNumberText.snp.left).offset(-10.0)
        }
        messageText.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(15.0)
            make.right.equalTo(giftImageView.snp.left).offset(-15.0)
        }
        if self.alpha == 0{
            UIView.animate(withDuration: 0.25) {
                self.alpha = 1
            }
        }
    }
    
    private func dismiss(){
        UIView.animate(withDuration: 0.25) {
            self.alpha = 0
        }
    }
}
